package com.qsmx.store.app.main.service;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.qsmx.store.app.R;
import com.qsmx.store.app.R2;
import com.qsmx.store.app.event.SetShowMaxEvent;
import com.qsmx.store.app.main.service.adapter.ServiceLeftAdapter;
import com.qsmx.store.app.main.service.entity.ServiceTitleEntity;
import com.qsmx.store.core.delegates.AtmosDelegate;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class ServiceHomeDelegate extends AtmosDelegate {

    ServiceLeftAdapter mAdapter;
    List<ServiceTitleEntity.ServiceTitleList> mList = new ArrayList<>();

    @BindView(R2.id.rlv_service_left)
    RecyclerView rlvServiceLeft = null;

    @Override
    public Object setLayout() {
        return R.layout.delegate_service_home;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {

        getSupportDelegate().loadRootFragment(R.id.delegate_container, ServiceCoinDelegate.create("out"));

        initData();
        initRecycler();

    }

    private void initRecycler() {

        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        mAdapter = new ServiceLeftAdapter(getContext());
        mAdapter.setData(mList);

        rlvServiceLeft.setLayoutManager(manager);
        rlvServiceLeft.setAdapter(mAdapter);

        mAdapter.setonItemClickListener(new ServiceLeftAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                if (position == 0) {
                    getSupportDelegate().replaceChildFragment(ServiceCoinDelegate.create("out"), false);
                } else if (position == 1) {
                    getSupportDelegate().replaceChildFragment(ServiceCoinDelegate.create("save"), false);
                } else if (position == 2) {
                    getSupportDelegate().replaceChildFragment(new ServiceMemChangeDelegate(), false);
                } else if (position == 3) {
                    getSupportDelegate().replaceChildFragment(new ServiceMemUpdateDelegate(), false);
                } else if (position == 4) {
                    getSupportDelegate().replaceChildFragment(new ServiceSaveTicketDelegate(), false);
                } else if (position == 5) {
//                    getSupportDelegate().replaceChildFragment(mServiceCoinDelegate, false);
                } else if (position == 6) {
//                    getSupportDelegate().replaceChildFragment(mServiceCoinDelegate, false);
                }

                for (ServiceTitleEntity.ServiceTitleList data : mList) {
                    data.setSelect(false);
                }

                mList.get(position).setSelect(true);

                if (!mAdapter.getIsShowMax()) {
                    mAdapter.setIsShowMax(true);
                }

                mAdapter.notifyDataSetChanged();
            }
        });

        rlvServiceLeft.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus){
                    mAdapter.setIsShowMax(true);
                }else {
                    mAdapter.setIsShowMax(false);
                }
                mAdapter.notifyDataSetChanged();
            }
        });


    }


    private void initData() {

        ServiceTitleEntity.ServiceTitleList data = new ServiceTitleEntity().new ServiceTitleList();
        ServiceTitleEntity.ServiceTitleList data1 = new ServiceTitleEntity().new ServiceTitleList();
        ServiceTitleEntity.ServiceTitleList data2 = new ServiceTitleEntity().new ServiceTitleList();
        ServiceTitleEntity.ServiceTitleList data3 = new ServiceTitleEntity().new ServiceTitleList();
        ServiceTitleEntity.ServiceTitleList data4 = new ServiceTitleEntity().new ServiceTitleList();
        ServiceTitleEntity.ServiceTitleList data5 = new ServiceTitleEntity().new ServiceTitleList();
        ServiceTitleEntity.ServiceTitleList data6 = new ServiceTitleEntity().new ServiceTitleList();

        data.setTitle(getString(R.string.service_get_coin));
        data.setIcon(R.mipmap.icon_get_coin);
        data.setSelect(true);

        data1.setTitle(getString(R.string.service_save_coin));
        data1.setIcon(R.mipmap.icon_save_coin);
        data1.setSelect(false);

        data2.setTitle(getString(R.string.service_mem_to_other));
        data2.setIcon(R.mipmap.icon_mem_to_other);
        data2.setSelect(false);

        data3.setTitle(getString(R.string.service_mem_update));
        data3.setIcon(R.mipmap.icon_mem_update);
        data3.setSelect(false);

        data4.setTitle(getString(R.string.service_save_ticket));
        data4.setIcon(R.mipmap.icon_save_ticket);
        data4.setSelect(false);

        data5.setTitle(getString(R.string.service_qr_code_verification));
        data5.setIcon(R.mipmap.icon_qr_code);
        data5.setSelect(false);

        data6.setTitle(getString(R.string.service_mem_account_query));
        data6.setIcon(R.mipmap.icon_mem_qurey);
        data6.setSelect(false);


        mList.add(data);
        mList.add(data1);
        mList.add(data2);
        mList.add(data3);
        mList.add(data4);
        mList.add(data5);
        mList.add(data6);

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(SetShowMaxEvent event) {
        if (event != null && event.getData() != null) {
            if (mAdapter.getIsShowMax()) {
                mAdapter.setIsShowMax(false);
                mAdapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
